function IF_Helper () {
	var tools = new Array();
	var toolbar;
	this.close = function () {
		$("#if_helper_body").hide();
		$("#if_helper_window").hide();
	}
	
	this.addTool = function (def) {
		def.id = this.tid(def.id);
		tools.push(new IF_Tool(def));
	}
	
	this.render = function () {
		for (var key in tools) {
			var tool = tools[key];
			toolbar.append("<div title=\"" + tool.desc + "\" id=\"" + tool.id  + "\">" + tool.name + "</div>");
			$("#" + tool.id)[0].tool = tool;
			$("#" + tool.id).click(function () {
				$("#if_helper_body div").removeClass("selected");
				$(this).addClass("selected");
				this.tool.createWindow(this.tool.width, this.tool.height, this.tool.url);
			});
			$("#" + tool.id).css("cursor", "pointer");
		}		
	}
	
	/**
	* initialize
	*/
	this.init = function () {
		//create helper toolbar body
		var left = $("body").innerWidth() - 200;
		if (!jQuery.support.boxModel) {
			left -= 35;
		}
		$("body").append("<div id=\"if_helper_body\" style=\"color:#000;background-color:#fff;position:absolute;border:1px #ccc solid;width:100px;padding:3px;left:" + left + "px;top:10px;\"><div style=\"background-color:#dedede;font-weight:bold;padding:2px\">&#26694;&#26550;&#21161;&#25163; <a href=\"#\" style=\"text-decoration:none;color:#000;font-size:10px;font-weight:normal\" onclick=\"ifHelper.close();return false;\" title=\"&#20851;&#38381;\">&#20851;&#38381;</a></div></div>");
		
		//add tools
		toolbar = $("#if_helper_body");
		this.addTool({
			"id": "context",
			"name": "&#19978;&#19979;&#25991;", 
			"desc": "&#26597;&#30475;&#19978;&#19979;&#25991;&#20013;&#30340;Cookie&#12289;SESSION&#31561;&#20449;&#24687;", 
			"url": "/if_helper_context",
			"width": 700,
			"height": 500
		});
		this.addTool({
			"id": "clean",
			"name": "&#28165;&#29702;", 
			"desc": "&#28165;&#29702;&#32531;&#23384;&#12289;&#26085;&#24535;&#12289;&#27169;&#26495;&#32534;&#35793;&#25991;&#20214;&#21644;&#31995;&#32479;&#32534;&#35793;&#25991;&#20214;", 
			"url": "/if_helper_clean",
			"width": 130,
			"height": 100
		});
		this.addTool({
			"id": "template",
			"name": "&#27169;&#26495;", 
			"desc": "&#21019;&#24314;&#25110;&#20462;&#25913;&#27169;&#26495;", 
			"url": "/if_helper_template?tpl=" + ifHelperTpl,
			"width": 700,
			"height": 500
		});		
		this.addTool({
			"id": "controller",
			"name": "&#25511;&#21046;&#22120;", 
			"desc": "&#32534;&#36753;&#25511;&#21046;&#22120;&#28304;&#30721;", 
			"url": "/if_helper_controller?file=" + ifHelperControllerFile,
			"width": 700,
			"height": 500
		});		
		this.addTool({
			"id": "model",
			"name": "&#27169;&#22411;", 
			"desc": "&#25968;&#25454;&#34920;&#27169;&#22411;&#31649;&#29702;", 
			"url": "/if_helper_model",
			"width": 700,
			"height": 500
		});	
		this.addTool({
			"id": "api",
			"name": "API&#26597;&#35810;", 
			"desc": "&#26597;&#35810;&#31867;&#21644;&#26041;&#27861;&#12289;&#20989;&#25968;&#30340;API", 
			"url": "/if_helper_api",
			"width": 700,
			"height": 500
		});
		/**this.addTool({
			"id": "filter",
			"name": "过滤器", 
			"desc": "过滤器管理", 
			"url": "/if_helper_filter",
			"width": 700,
			"height": 500
		});**/		
		this.addTool({
			"id": "config_database",
			"name": "&#25968;&#25454;&#24211;", 
			"desc": "&#25968;&#25454;&#24211;&#37197;&#32622;", 
			"url": "/if_helper_config_database",
			"width": 430,
			"height": 330
		});	
		this.addTool({
			"id": "config_message",
			"name": "&#21306;&#22495;&#21270;", 
			"desc": "&#37197;&#32622;&#21306;&#22495;&#21270;&#28040;&#24687;", 
			"url": "/if_helper_config_message",
			"width": 700,
			"height": 500
		});
		this.addTool({
			"id": "log",
			"name": "&#26085;&#24535;", 
			"desc": "&#26597;&#30475;&#31995;&#32479;&#35843;&#35797;&#26085;&#24535;", 
			"url": "/if_helper_log",
			"width": 700,
			"height": 500
		});	
		this.addTool({
			"id": "module",
			"name": "&#27169;&#22359;/&#25554;&#20214;", 
			"desc": "&#31649;&#29702;&#24050;&#32463;&#23433;&#35013;&#30340;&#27169;&#22359;", 
			"url": "/if_helper_module",
			"width": 700,
			"height": 500
		});		
		this.addTool({
			"id": "project",
			"name": "&#39033;&#30446;", 
			"desc": "&#20851;&#20110;&#39033;&#30446;&#30340;&#29256;&#26412;&#12289;&#29615;&#22659;&#31561;&#20449;&#24687;", 
			"url": "/if_helper_project",
			"width": 400,
			"height": 200
		});		
		this.render();
	}	
	
	this.tid = function (toolId) {
		return "if_helper_toolbar_" + toolId;
	}
}

function IF_Tool(def) {
	for (var key in def) {
		this[key] = def[key];
	}
	
	this.handler = function () {
		return $("#" + this.id);
	}
	
	this.createWindow = function (width, height, src) {
		var windowId = "if_helper_window";
		var div = $("#" + windowId);
		if (!div[0]) {
			div = $(document.createElement("DIV"));
			div.attr("id", windowId);
			div.appendTo(document.body);
		}
		else {
			if (div.is(":visible") && div.attr("tool_id") == this.id) {
				div.hide();	
			}
			else {
				div.show();
			}
		}
		div.css("z-index", 9999999);
		div.css("background-color", "#fff");
		div.css("border", "2px #ccc solid");
		div.css("width", width + "px");
		div.css("height", height + "px");
		div.css("top", $("#if_helper_body").offset().top + "px");
		div.css("left", ($("#if_helper_body").offset().left - width - 5) + "px");
		div.css("position", "absolute");
		div.attr("tool_id", this.id);
		if (src) {
			src = ifHelperRouter + src;
			var r = "if_helper_page" + Math.random();//fk firefox again
			div.html("<div id=\"if_helper_loading\" style=\"font-size:14px;color:blue\">&nbsp;加载中，请稍候...</div><iframe src=\"\" id=\"if_helper_page\" name=\"" + r + "\"  marginheight=0 marginwidth=0 hspace=0 vspace=0 frameborder=\"0\" width=\"100%\" height=\"100%\"></iframe>");
			window.frames[r].document.location.replace(src);
		}
		return div;
	}
}